<template>
  <!--闪电下单-->
  <div style="height: 100%;border: #898C95 solid 1px;margin-left: 2px;">
    <div>
      <div class="transaction_in">
        <div class="transaction_in_right">
          <div>
            <div class="divTab">
              <div class="Twokey w350">
                <span class="twokey_title">合约</span>
                <el-select class="" size="mini" @change="getPanelData" v-model="namesp" clearable placeholder="请选择">
                  <el-option
                    v-for="(item, inx) in ticket"
                    :key="inx"
                    :label="item.M + item.S + item.C"
                    :value="item.M + item.S + item.C">
                  </el-option>
                </el-select>

                <div class="twokey_right"></div>
              </div>
              <div class="Twokey_divin view">
                 <div class="Twokey view flex-row w350">
                    <div class="twokey2_title">数量</div>
                    <el-input class="twokey2_hands " v-model="Numberhand" type="number" name="number" value="1" min="1" size="mini"/>
                    <div class="m-l-5">手</div>
                  </div>
                  
                   <div class="view flex-row m-l-135">
                      <span style="color:#FF0000;">买:</span>
                      <span style="color: #000;">可开--  </span>
                      <span>可平 0</span>
                    </div>
                    <div class="view flex-row m-l-135">
                      <span style="color: #228B22;">卖:</span>
                      <span style="color: #000;">可开-- </span>
                      <span style="color: #000;">可平 0</span>
                    </div>
                  <div class="Twokey view flex-row">
                    <div class="m-l-20 fz-12 w50">价格： </div>
                    <el-autocomplete
                        class="inline-input"
                        v-model="priceTxt"
                        :fetch-suggestions="querySearch"
                        placeholder="请选择价格"
                        @select="handleSelect"
                      ></el-autocomplete> 
                   
                  </div>
               
                  <div style="display: flex;justify-content: flex-start;width: 100%;" class="view">
                    <div class="Twokey_isn_right">
                      <div class="reset1" @click="fuwei"><span>复 位</span></div>
                    </div>

                    <div class="Business view flex-row width100">
                      <div class="Business_left flex-1" @click="btnlist && business(selldis)">
                        <span class="Business_left_font1">{{ selldis || '- - ' }}</span>
                        <div class="Business_left_bor"></div>
                        <span class="Business_left_font1">买&nbsp;入</span>
                      </div>
                      <div class="Business_right flex-1" @click="btnlist && selldiss(buydis)">
                        <span class="Business_left_font2">{{ buydis || '- - ' }}</span>
                        <div class="Business_left_bor2"></div>
                        <span class="Business_left_font2">卖&nbsp;出</span>
                      </div>
                    </div>
                    
                  </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import three from '@/components/foot/three.vue'; //三键
import tradition from '@/components/foot/tradition.vue'; //传统
import { mapState } from 'vuex';
export default {
  computed: mapState({
    hoidlist: state => state.hoidlist,
    select: state => state.trade.select /* 当前选中股指 */,
    symbol: state => state.trade.symbol /* 全部行情 */,
    symbos: state => state.trade.symbos /* 当前页面显示杨青 */,
    ticket: state => state.trade.ticket /* 推送数据 */,
    optionlist: state => state.trade.optionlist /* 全部合约代码 */
  }),
  watch: {
    // namesp(val) {
    //   for (let i = 0; i < this.list.length; i++) {
    //     if (val == this.list[i].code2) {
    //       return this.$store.commit('trade/select', this.list[i]);
    //     }
    //   }
    // },
    // price(val) {
    //   // console.log(val);
    //   if (this.priceprict == false) {
    //     this.selldis = val; //卖价显示
    //     this.buydis = val;
    //   }
    // },
    ticket(val) {
      if (this.priceprict == true) {
        const itemData = this.ticket.find(item => (item.M + item.S + item.C) == this.namesp)
        if (itemData && itemData.S1) {
          this.selldis = itemData.S1
          this.buydis = itemData.B1
        }
        
      }
    },
    select: {
      handler: function(val) {
          this.namesp = val;
          if (this.cmd == 'tra') {
            const itemData = this.ticket.find(item => (item.M + item.S + item.C) == this.namesp)
            if (itemData) {

            this.priceTxt = itemData.S1
            }
          }
          
　　　　},
      immediate: true,
　　　 deep:true
    }
  },
  data() {
    return {
      cmd: 'now',
      btnlist: true,
      flat: '',
      gold: '',
      priceType: 1,
      priceTxt: '对手价',
      pricechoose: [
        {
          value: '对手价',
          val: 1
        }, {
          value: '排队价',
          val: 2
        }, {
          value: '市价',
          val: 3
        }, {
          value: '最新价',
          val: 4
        }, {
          value: '超价',
          val: 5
        }
      ],
      list: [], //合约名称
      rise_fall: 'rise', // 方向
      opentype: 'open', // 开平
      oneitem: {},
      tabsParam: ['闪电下单', '三键下单', '传统下单'],
      nowIndex: 0,
      namesp: '', //合约
      radioVal: '1', //开仓类型
      Numberhand: '', //手数
      price: '', //价格
      priceprict: true, //买卖价格变动
      typename: '', //投机类型
      selldis: '', //卖价
      buydis: '', //买家
      profit_price: 0 /* 止盈价格 */,
      loss_price: 0 /* 止损价格 */,
      radio1: '',
      code: '', // 合约编码
      // disscond: false, //条件单
    };
  },
  components: { three, tradition },
  created: function() {
    for (let i in this.optionlist) {
      this.list.push(this.optionlist[i]);
    }
    // console.log(this.list)
    // this.list = this.optionlist;
    this.price = ((this.ticket[this.select.code2] && this.ticket[this.select.code2].price) || 0).toFixed(this.select.digit);
    // this.buydis = ((this.ticket[this.select.code2] && this.ticket[this.select.code2].bid) || 0).toFixed(this.select.digit);
    // this.selldis = ((this.ticket[this.select.code2] && this.ticket[this.select.code2].ask) || 0).toFixed(this.select.digit);
    this.buydis = ((this.ticket[this.select.code2] && this.ticket[this.select.code2].price) || 0).toFixed(this.select.digit);
    this.selldis = ((this.ticket[this.select.code2] && this.ticket[this.select.code2].price) || 0).toFixed(this.select.digit);
  },
  mounted() {},
  methods: {
    handleSelect(item) {
      this.priceType = item.val
    },
    querySearch(queryString, cb) {
      cb(this.pricechoose);
    },
    getPanelData() {   
      const itemData = this.ticket.find(item => (item.M + item.S + item.C) == this.namesp)
      this.selldis = itemData.S1
      this.buydis = itemData.B1
    },
    fuwei() {
      this.Numberhand = 1
      this.priceprict = true
    },
    //切换
    toggleTabs(index) {
      this.nowIndex = index;
      if (this.nowIndex == 0) {
        this.cmd = 'now'
      } else if (this.nowIndex == 1) {
        this.cmd = 'third'
      } else {
        const itemData = this.ticket.find(item => (item.M + item.S + item.C) == this.namesp)
        if (itemData) {
          this.priceTxt = itemData.S1
        }
        
        this.cmd = 'tra'
      }
       
    },
    /* 价格输入方式切换 */
    changeprice() {
      this.priceprict = false;
    },
    /* 切换select*/
    changeslect(code) {
      this.priceprict = true;
      for (let i = 0; i < this.list.length; i++) {
        if (code == this.list[i].name_code) {
          return this.$store.commit('trade/select', this.list[i]);
        }
      }
    },
    //三键平仓
    closeposition() {
      if (this.hoidlist == null || this.hoidlist == '') {
        return this.$notify.error('请选择要平仓的订单');
      }
      this.$post('order/saleorder', { symbol_id: this.hoidlist.contract,rise_fall:this.hoidlist.risefall  }).then(res => {
        if (res.code == 1) {
          that.$emit('business')
          this.$notify.success(res.msg || '平仓成功');
        } else {
          this.$notify.error(res.msg || '开仓失败');
        }
      });
    },
    //买入
    business(num) {
      this.btnlist = false;
      if (this.Numberhand == '') {
        this.btnlist = true;
        return this.$notify.error('请输入手数');
      }

      if (this.priceprict == false) {
        // if (this.selldis == this.price) {
        //   var buylist = {
        //     symbol_id: this.select.code2,
        //     cmd: 'NOW',
        //     rise_fall: 'RISE',
        //     price: num,
        //     volume: this.Numberhand,
        //     stop_profit_price: this.profit_price /* 止盈价格 */,
        //     stop_loss_price: this.loss_price /* 止损价格 */
        //   };
        // } else {
        //   var buylist = {
        //     symbol_id: this.select.code2,
        //     cmd: 'NOW',
        //     rise_fall: 'RISE',
        //     // price: num,
        //     volume: this.Numberhand,
        //     stop_profit_price: this.profit_price /* 止盈价格 */,
        //     stop_loss_price: this.loss_price /* 止损价格 */
        //   };
        // }
      } else {
        var buylist = {
          symbol_id: this.namesp,
          cmd: this.cmd,
          rise_fall: 'rise',
          pricetype: this.pricetype,
          // price: num,
          volume: this.Numberhand,
          stop_profit_price: this.profit_price /* 止盈价格 */,
          stop_loss_price: this.loss_price /* 止损价格 */
        };
      }
      var that = this;
      var bool = false;
      this.$layer.open({
        content: '确定买入' + this.Numberhand + '手' + this.namesp + ', 是否继续?',
        btn: ['确定', '取消'],
        yes: function(index, layero) {
          //按钮【按钮一】的回调
          if (!bool) {
            bool = true;

            that.$post('order/openstock', buylist).then(res => {
              // console.log(res);
              that.$layer.closeAll();
              that.btnlist = true;
              bool = false;
              if (res.code == 1) {
                that.$notify.success(res.message || '开仓成功');
                that.$emit('business')
              } else {
                that.$notify.error(res.message || '开仓失败');
              }
            });
          }
        },
        btn2: function(index, layero) {
          that.$layer.closeAll();
          that.btnlist = true;
          that.$layer.msg('已取消');
        },
        cancel: function() {
          //右上角关闭回调
          that.btnlist = true;
          that.$layer.closeAll();
          that.$layer.msg('已取消');
          //return false 开启该代码可禁止点击该按钮关闭
        }
      });
    },
    /* 卖出 */
    selldiss(num) {
      this.btnlist = false;
      if (this.Numberhand == '') {
        this.btnlist = true;
        return this.$notify.error('请输入手数');
      }
      if (this.priceprict == false) {
        // if (this.selldis == this.price) {
        //   var buylist = {
        //     symbol_id: this.select.code2,
        //     cmd: 'NOW',
        //     rise_fall: 'fall',
        //     price: num,
        //     volume: this.Numberhand,
        //     stop_profit_price: this.profit_price /* 止盈价格 */,
        //     stop_loss_price: this.loss_price /* 止损价格 */
        //   };
        // } else {
        //   var buylist = {
        //     symbol_id: this.select.code2, // 合约代码
        //     cmd: 'NOW', //闪电下单 三建下单 传统下单
        //     rise_fall: 'fall',
        //     // price: num,
        //     volume: this.Numberhand,
        //     stop_profit_price: this.profit_price /* 止盈价格 */,
        //     stop_loss_price: this.loss_price /* 止损价格 */
        //   };
        // }
      } else {
        var buylist = {
          symbol_id: this.namesp,
          cmd: this.cmd,
          rise_fall: 'fall',
          pricetype: this.pricetype,
          // price: num,
          volume: this.Numberhand,
          stop_profit_price: this.profit_price /* 止盈价格 */,
          stop_loss_price: this.loss_price /* 止损价格 */
        };
      }
      var that = this;
      var bool = false;
      this.$layer.open({
        content: '确定卖出' + this.Numberhand + '手' + this.namesp + ', 是否继续?',
        btn: ['确定', '取消'],
        yes: function(index, layero) {
          //按钮【按钮一】的回调
          if (!bool) {
            bool = true;

            that.$post('addons/tf-futures/order/open-with-price', buylist).then(res => {
              // console.log(res);
              that.$layer.closeAll();
              that.btnlist = true;
              bool = false;
              if (res.code == 200) {
                that.$notify.success(res.message || '开仓成功');
              } else {
                that.$notify.error(res.message || '开仓失败');
              }
            });
          }
        },
        btn2: function(index, layero) {
          that.$layer.closeAll();
          that.btnlist = true;
          that.$notify({
            type: 'info',
            message: '已取消购买'
          });
        },
        cancel: function() {
          //右上角关闭回调
          that.btnlist = true;
          that.$layer.closeAll();
          that.$layer.msg('已取消');
          //return false 开启该代码可禁止点击该按钮关闭
        }
      });
    }
  }
};
</script>
<style scoped>
.tabs {
  height: 23px;
  color: #333333;
  display: flex;
  justify-content: flex-start;
}

.li-tab {
  cursor: pointer;
  width: 100%;
  height: 100%;
  line-height: 25px;
  display: inline-block;
  text-align: center;
  font-size: 12px;
  background-image: linear-gradient(to bottom, #f2f2f2, #ececec, #d1d1d1, #cfcfcf);
}

.divTab {
  width: 100%;
}

.active {
  background: #ffffff;
  color: #333333;
  border-bottom: none;
}

.Twokey {
  height: 40px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.Business_right2 {
  cursor: pointer;
  width: 105px;
  height: 55px;
  background-image: linear-gradient(to bottom, #ffffff, #f9f9f6, #f1f1eb);
  border-radius: 3px;
  margin-left: 10px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  border: #003c74 solid 1px;
}
.twokey_title {
  font-size: 12px;
  color: #333333;
  margin-left: 5%;
  margin-right: 16px;
}

.Twokey_divin {
  width: 100%;
}
.Twokey2 {
  float: left;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.twokey2_title,.twokey_title {
  font-size: 12px;
  color: #333333;
  margin-left: 5%;
  margin-right: 16px;
  width: 40px;
}

.twokey_inp {
  width: 50%;
  height: 23px;
}

.twokey_right {
  width: 4px;
  height: 23px;
  /* background-color: #BBBBBB; */
  /* border: #999999 solid 1px; */
  margin-left: 8px;
}

.radiodiv {
  padding-left: 16px;
  box-sizing: border-box;
  font-size: 12px;
  display: flex;
  text-justify: flex-start;
  align-items: center;
}
.Business_left_font3 {
  color: #dda610;
  font-size: 16px;
  font-weight: bold;
}
.Business_left_bor1 {
  width: 85px;
  height: 1px;
  background-color: #808080;
  margin-top: 3px;
  /* margin-bottom: 0.3rem; */
}
.radiodiv_Purchase {
  color: #e15546;
  margin-left: 45px;
}

.radiodiv_Sellout {
  color: #3f9e0e;
}
.twokey_handa {
  width: 20%;
  height: 23px;
  border-radius: 2px;
  padding-left: 5px;
}
.twokey_hands {
  width: 40%;
  height: 23px;
  border-radius: 2px;
  padding-left: 5px;
}

.Twokey_in {
  width: 100%;
}

.Business {
  height: 65px;
  margin-top: 15px;
  display: flex;
  justify-content: flex-start;
  padding-right: 15px;
  box-sizing: border-box;
}

.Business_left {
  height: 55px;
  background-image: linear-gradient(to bottom, #ffffff, #f9f9f6, #f1f1eb);
  border-radius: 3px;
  margin-left: 10px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  border: #003c74 solid 1px;
  font-weight: bold;
  cursor: pointer;
}

.Business_right {
  width: 105px;
  height: 55px;
  border-radius: 3px;
  margin-left: 10px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  background-image: linear-gradient(to bottom, #ffffff, #f9f9f6, #f1f1eb);
  border: #003c74 solid 1px;
  font-weight: bold;
  cursor: pointer;
}

.Business_left_bor {
  width: 70px;
  height: 1px;
  background-color: #808080;
  margin-top: 3px;
}
.Business_left_bor2 {
  width: 70px;
  height: 1px;
  background-color: #808080;
  margin-top: 3px;
}

.Business_left_font1 {
  color: #bd0909;
  font-size: 16px;
  font-weight: bold;
}
.Business_left_font2 {
  color: #088a02;
  font-size: 16px;
  font-weight: bold;
}



.isn_right_mai {
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: flex-start;
  padding-left: 16px;
  box-sizing: border-box;
  align-items: center;
}

.isn_right_mai_left {
  width: 25px;
  height: 25px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #e15546;
  margin-right: 8px;
}

.isn_right_mai_rissght {
  width: 25px;
  height: 25px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #3f9e0e;
  margin-right: 8px;
}

.isn_right_mai_right {
  width: 80px;
  height: 35px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-sizing: border-box;
  color: #333333;
  font-size: 12px;
}

.reset1 {
  cursor: pointer;
  width: 65px;
  height: 25px;
  border: #003c74 solid 1px;
  background-image: linear-gradient(to bottom, #fefefe, #f6f6f2, #e2dfd6);
  margin-left: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #333333;
  font-size: 12px;
  border-radius: 5px;
}
.reset {
  width: 50%;
  height: 25px;
  border: #003c74 solid 1px;
  background-image: linear-gradient(to bottom, #fefefe, #f6f6f2, #e2dfd6);
  margin-left: 8px;
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #333333;
  font-size: 12px;
  border-radius: 5px;
}
/*条件单*/
.stoppo {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  /* background-color: red; */
  background-color: rgba(0, 0, 0, 0.2);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 101;
}
</style>
